package com.shuoyue.ycgk.ui.papergroups;

import android.os.Bundle;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.base.BaseMvpFragment;
import com.shuoyue.ycgk.base.ListWithPage;
import com.shuoyue.ycgk.entity.Paper;
import com.shuoyue.ycgk.entity.PaperSet;
import com.shuoyue.ycgk.entity.Type;
import com.shuoyue.ycgk.ui.papergroups.adapter.PaperSetAdapter;
import com.shuoyue.ycgk.utils.SizeUtil;
import com.shuoyue.ycgk.utils.SpacesItemDecoration;

import java.util.List;

import butterknife.BindView;

public abstract class BaseFragmentPaperGroup extends BaseMvpFragment<PaperContract.Presenter>
        implements PaperContract.View {


    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.refreshlayout)
    SmartRefreshLayout refreshlayout;

    protected Type type;
    public String title;
    PaperSetAdapter paperSetAdapter;
    protected boolean isMine;
    protected int queryType;//查询类型 1:历年真题  -1:题库 2:时政热点  3:自由组卷  4:我的题集

    @Override
    public void initParams(Bundle bundle) {
        type = (Type) bundle.getSerializable("type");
        isMine = bundle.getBoolean("isMine", false);
        queryType = bundle.getInt("queryType", -1);

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_paper_set;
    }

    @Override
    protected void initView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
//        recyclerView.addItemDecoration(new SpacesItemDecoration(SizeUtil.dip2px(mContext, 4), false));
        refreshlayout.setEnableLoadMore(false);
        refreshlayout.setOnRefreshListener(refreshLayout -> {
            mPresenter.getPaperSet(type.getId(), null,
                    queryType == -1 ? null : queryType
                    , isMine);
            refreshLayout.finishRefresh(2000);
        });

        paperSetAdapter = new PaperSetAdapter(null);
        recyclerView.setAdapter(paperSetAdapter);
        paperSetAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            PaperSet paperSet = (PaperSet) adapter.getItem(position);
            onItemClick(paperSet);
        });

        registEmptyView(paperSetAdapter);
    }

    protected abstract void onItemClick(PaperSet paperSet);

    @Override
    protected void initData() {
        mPresenter = new PaperContract.Presenter();
        mPresenter.attachView(this);
        mPresenter.getPaperSet(type.getId(), null, queryType == -1 ? null : queryType
                , isMine);
    }


    @Override
    public String getTitle() {
        return title;
    }

    @Override
    public void setPaperSet(List<PaperSet> paperSets) {
        paperSetAdapter.resetData(paperSets);
        refreshlayout.finishRefresh();
        refreshlayout.finishLoadMore();
    }

    @Override
    public void setPaperSetInfo(PaperSet paperInfo) {

    }

    @Override
    public void setPaperList(ListWithPage<Paper> listWithPage) {

    }

    @Override
    public void setPaperSet(ListWithPage<PaperSet> paperSets) {

    }
}
